<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    >

    <xsl:output
        method="xml"
        media-type="application/xhtml+xml"
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
        omit-xml-declaration="no"
        />

    <xsl:param name="arg1"/>
    <xsl:param name="arg2"/>
    <xsl:param name="user"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>IOVAR Demo</title>
                <meta name="viewport" content="width=device-width" />
                <link rel="stylesheet" type="text/css" href="/usr/include/ui/general.css"/>
                <link rel="stylesheet" type="text/css" href="/usr/include/ui/dashboard.css"/>
                <link rel="stylesheet" type="text/css" href="/usr/include/ui/edit.css"/>
                <script language="JavaScript" type="text/javascript" src="/usr/include/ui/general.js">/* */</script>
                <script language="JavaScript" type="text/javascript" src="/usr/include/ui/async.js">/* */</script>
                <script language="JavaScript" type="text/javascript" src="/usr/include/ui/edit.js">/* */</script>
            </head>

            <body onload="document.onkeyup=closePopupOnEscape">
                <h1>IOVAR Demo</h1>

                <xsl:if test="$user = ''">
                    <ul id="meta">
                        <li><a target="shell" href="/$">shell</a></li>
                        <li><a target="log" href="/var/log/shell">log</a></li>
                    </ul>

                    <p class="legacy"/>
                    <ul id="meta2">
                        <li><a href="/$/sbin/setview?=xhtml&amp;=index">xhtml</a></li>
                        <li><a target="xml" href="/$/sbin/setview?=xml&amp;=index">xml</a></li>
                    </ul>

                    <p class="legacy"/>
                </xsl:if>

                <ul id="meta3">
                    <li><a target="changelog" href="/CHANGELOG">CHANGELOG</a></li>
                </ul>

                <ul id="user">
                    <li><a href="/home/index.shtml">Logout</a></li>
                </ul>

                <img id="header" src="/usr/include/header.png"/>

                <ul id="social">
                    <li><a target="facebook" href="http://facebook.com/LannoccTech" title="Visit us on Facebook"><img src="/usr/include/facebook.png"/></a></li>
                </ul>

                <ul id="menu">
                    <li><a class="current" href="/$/index">Demo</a></li>
                </ul>

                <script type="text/javascript" language="javascript">
                    var pop_wrap = document.createElement ('div');
                    pop_wrap.id = 'popup_wrap';

                    var pop_head = document.createElement ('div');
                    pop_head.id = 'popup_header';

                    var pop_anchor = document.createElement ('a');
                    pop_anchor.name = 'popup';
                    pop_head.appendChild (pop_anchor);

                    var pop_close = document.createElement ('img');
                    pop_close.id = 'popup_close';
                    pop_close.title = 'Close (ESC key)';
                    pop_close.alt = 'close button';
                    pop_close.src = '/usr/include/ui/close.png';
                    pop_close.onclick = closePopup;
                    pop_head.appendChild (pop_close);

                    var pop = document.createElement ('fieldset');
                    pop.id = 'popup';

                    pop.appendChild (pop_head);
                    pop_wrap.appendChild (pop);
                    document.body.appendChild (pop_wrap);

                    /* **** */

                    pop_wrap = document.createElement ('div');
                    pop_wrap.id = 'popup2_wrap';

                    pop_head = document.createElement ('div');
                    pop_head.id = 'popup2_header';

                    pop_anchor = document.createElement ('a');
                    pop_anchor.name = 'popup2';
                    pop_head.appendChild (pop_anchor);

                    pop_close = document.createElement ('img');
                    pop_close.id = 'popup2_close';
                    pop_close.title = 'Close (ESC key)';
                    pop_close.alt = 'close button';
                    pop_close.src = '/usr/include/ui/close.png';
                    pop_close.onclick = closePopup;
                    pop_head.appendChild (pop_close);

                    pop = document.createElement ('fieldset');
                    pop.id = 'popup2';

                    pop.appendChild (pop_head);
                    pop_wrap.appendChild (pop);
                    document.body.appendChild (pop_wrap);
                </script>
                <noscript>
                    <p>Asynchronous popups and data loading disabled due to lack of javascript support.</p>
                </noscript>

                <hr class="legacy"/>

                <div id="body">

                    <xsl:apply-templates/>

                    <sub id="footer">
                        <iframe id="async" name="async">
                            <p>Asynchronous form updates disabled due to lack of iframe support.</p>
                        </iframe>

                        <img src="/usr/include/footer.jpg"/><br/>
                        Copyright (C) 2011-2015 Lannocc Technologies<br/>
                        Design &amp; Hosting by <a target="lannocc" href="http://lannocc.com" title="Web Sites. Business IT. Computer Service. Located in Billings, Montana.">Lannocc Technologies</a>
                    </sub>
                </div>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="/iovar-demo">
        <h2>IOVAR Demo Application</h2>

        <!--
        <fieldset class="data">
            <legend>import</legend>

            <ul>
                <li><a href="/$/bin/customer/import">customer import bulk cleanup</a></li>
                <li><a href="/$/bin/customer/addresses">customer addresses bulk setup</a></li>
            </ul>
        </fieldset>
        -->

        <p><a href="/$/bin/log/view">activity log</a></p>

        <xsl:apply-templates select="order-list"/>

        <fieldset class="data">
            <legend><h3>code tables</h3></legend>

            <div>
                <ul>
                    <li><a href="/$/bin/data/ls?=action">log actions</a></li>
                    <li><a href="/$/bin/data/ls?=gender">genders</a></li>
                </ul>
            </div>
        </fieldset>

        <fieldset class="actions export">
            <legend><h3>exports</h3></legend>
            <ul>
                <li><a class="button" href="/$/bin/customer/maillist" title="Customer Mailing List">Mailing List</a></li>
            </ul>
        </fieldset>

        <!--
        <fieldset class="actions labels">
            <legend><h3>labels</h3></legend>
            <ul>
                <li><a class="button" target="item_tags" href="/$/bin/item/labels" title="Item Labels">Item Tags</a></li>
            </ul>
        </fieldset>
        -->

        <xsl:apply-templates select="*[not(self::order-list)]"/>

    </xsl:template>

    <xsl:template match="/log-list">
        <h2>Activity Log</h2>

        <table>
            <tr>
                <th>Date &amp; Time</th>
                <th>User</th>
                <th>Activity</th>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>

    <xsl:template match="/order">
        <h2>Order #<xsl:value-of select="id"/></h2>

        <xsl:variable name="paid">
            <xsl:choose>
                <xsl:when test="count(order-payment-list/order-payment) &gt; 0">
                    <xsl:value-of select="sum(order-payment-list/order-payment/Amount)"/>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:choose>
            <xsl:when test="Shipping != ''">
                <xsl:choose>
                    <xsl:when test="$paid &lt; Shipping + Subtotal + Processing">
                        <h3 class="due">Balance Due</h3>
                        <p class="due">$<xsl:value-of select="format-number(Shipping + Subtotal + Processing - $paid, '#,##0.00')"/></p>
                    </xsl:when>
                    <xsl:otherwise>
                        <h3 class="paid">Paid</h3>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <h3 class="due">Balance Due</h3>
                <p class="due">$<xsl:value-of select="format-number(Subtotal + Processing - $paid, '#,##0.00')"/> + Shipping</p>
            </xsl:otherwise>
        </xsl:choose>

        <h3>Status</h3>
        <ul>
            <li>Order placed / confirmed: <xsl:value-of select="Confirmed"/></li>
            <xsl:if test="Shipped != ''"><li>Shipped: <xsl:value-of select="Shipped"/></li></xsl:if>
            <xsl:if test="Closed != ''"><li>Closed: <xsl:value-of select="Closed"/></li></xsl:if>
        </ul>

        <xsl:if test="Closed = '' and Confirmed != '' and Shipped != '' and Shipping != '' and $paid &gt;= Shipping + Subtotal + Processing">
            <p><a class="button" href="/$/bin/order/close?={id}">Close Order</a></p>
        </xsl:if>

        <xsl:apply-templates select="consumer"/>
        <xsl:apply-templates select="order-item-list"/>
        <xsl:apply-templates select="order-payment-list"/>
    </xsl:template>

    <xsl:template match="consumer">
        <h3>Consumer</h3>

        <ul>
            <li>Name: <xsl:value-of select="First-Name"/><xsl:text> </xsl:text><xsl:value-of select="Last-Name"/></li>
            <xsl:if test="Company != ''">Company: <xsl:value-of select="Company"/></xsl:if>
            <li>Phone: <xsl:value-of select="Phone"/></li>
            <li>Email: <a target="email" href="mailto:{Email}"><xsl:value-of select="Email"/></a></li>
        </ul>
    </xsl:template>

    <xsl:template match="order-item-list">
        <h3>Items</h3>

        <table>
            <tr>
                <th class="item">Item #</th>
                <th class="description">Description</th>
                <th class="amount">Price</th>
            </tr>
            <xsl:apply-templates/>
            <tr class="subtotal">
                <td colspan="2" class="description">Subtotal:</td>
                <td class="amount">$<xsl:value-of select="format-number(sum(order-item/Price), '#,##0.00')"/></td>
            </tr>
            <tr class="fee">
                <td colspan="2" class="description">Processing Fee:</td>
                <td class="amount">$<xsl:value-of select="format-number(../Processing, '#,##0.00')"/></td>
            </tr>
            <tr class="shipping">
                <td colspan="2" class="description">Shipping:</td>
                <td class="amount"><xsl:if test="../Shipping != ''">$<xsl:value-of select="format-number(../Shipping, '#,##0.00')"/></xsl:if></td>
            </tr>
            <tr class="total">
                <td colspan="2" class="description">Grand Total:</td>
                <td class="amount"><xsl:if test="../Shipping != ''">$<xsl:value-of select="format-number(../Subtotal + ../Processing + ../Shipping, '#,##0.00')"/></xsl:if></td>
            </tr>
        </table>

        <p>Ship To:</p>
        <pre>
            <xsl:value-of select="../consumer/mailing/address/Address"/><br/>
            <xsl:value-of select="../consumer/mailing/address/City"/>, <xsl:value-of select="../consumer/mailing/address/State"/><xsl:text> </xsl:text><xsl:value-of select="../consumer/mailing/address/Zip"/>
        </pre>

        <xsl:if test="../Shipping = ''">
            <p><a class="button" href="/$/bin/order/ship?={../id}" onclick="return !loadInPopup(this)">Ship Order</a></p>
        </xsl:if>
    </xsl:template>

    <xsl:template match="order-item">
        <tr>
            <td class="item"><a target="item" href="/$/bin/item/view?={item/id}" onclick="return !loadInPopup (this)"><xsl:value-of select="item/id"/></a></td>
            <td class="description"><a target="part" href="/$/usr/bin/shopping/part?={item/part/id}"><xsl:value-of select="item/part/Description"/> (<xsl:value-of select="item/condition/Name"/>)</a></td>
            <td class="amount">$<xsl:value-of select="Price"/></td>
        </tr>
    </xsl:template>

    <xsl:template match="order-payment-list">
        <h3>Payments</h3>

        <table>
            <tr>
                <th class="received">Received</th>
                <th class="method">Method</th>
                <th class="amount">Amount</th>
            </tr>
            <xsl:apply-templates/>
            <tr class="subtotal">
                <td colspan="2" class="description">Total Received:</td>
                <td class="amount">$<xsl:value-of select="format-number(sum(order-payment/Amount), '#,##0.00')"/></td>
            </tr>
        </table>

        <p>Billing Address:</p>
        <pre>
            <xsl:value-of select="../consumer/billing/address/Address"/><br/>
            <xsl:value-of select="../consumer/billing/address/City"/>, <xsl:value-of select="../consumer/billing/address/State"/><xsl:text> </xsl:text><xsl:value-of select="../consumer/billing/address/Zip"/>
        </pre>

        <xsl:variable name="receive">
            <xsl:choose>
                <xsl:when test="../Shipping != ''">
                    <xsl:if test="count(order-payment) &lt; 1 or sum(order-payment/Amount) &lt; ../Shipping + ../Subtotal + ../Processing">1</xsl:if>
                </xsl:when>
                <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <p>
            <xsl:if test="$receive = 1">
                <a class="button" href="/$/bin/order/payment?={../id}" onclick="return !loadInPopup(this)">Receive Cash / Check / M.O.</a>
            </xsl:if>

            <xsl:if test="../stripe_card != '' and ../Shipping != '' and sum(order-payment/Amount) &lt; ../Shipping + ../Subtotal + ../Processing">
                <a class="button" href="/$/bin/order/charge?={../id}" onclick="return !loadInPopup(this)">Charge Shipping to Card</a>
            </xsl:if>
        </p>
    </xsl:template>

    <xsl:template match="order-payment">
        <tr>
            <td class="received"><xsl:value-of select="Received"/></td>
            <td class="method">
                <xsl:choose>
                    <xsl:when test="stripe_charge != ''">
                        Charge: <a target="stripe" href="https://dashboard.stripe.com/payments/{stripe_charge}" title="View charge details"><xsl:value-of select="stripe_charge"/></a>
                    </xsl:when>
                    <xsl:otherwise>
                        Cash / Check / M.O.
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="amount">$<xsl:value-of select="format-number(Amount, '#,##0.00')"/></td>
        </tr>
    </xsl:template>

    <xsl:template match="order-list">
        <fieldset class="data orders">
            <legend><h3>open orders</h3></legend>
            <div class="content">
                <ul>
                    <xsl:apply-templates/>
                </ul>
            </div>
        </fieldset>
    </xsl:template>

    <xsl:template match="order">
        <li>
            <a href="/$/bin/order/view?={id}" title="View Order">
                #<xsl:value-of select="id"/> - <xsl:value-of select="consumer/First-Name"/><xsl:text> </xsl:text><xsl:value-of select="consumer/Last-Name"/> - $<xsl:value-of select="format-number(Subtotal + Processing, '#,##0.00')"/>
                <xsl:text> </xsl:text>
                <xsl:choose>
                    <xsl:when test="Shipped != ''">[shipped]</xsl:when>
                    <xsl:otherwise>[new]</xsl:otherwise>
                </xsl:choose>
            </a>
        </li>
    </xsl:template>

    <xsl:template match="log">
        <tr>
            <td><xsl:value-of select="time"/></td>
            <td><xsl:value-of select="user"/></td>
            <td>
                <xsl:choose>
                    <xsl:when test="action = 10">created</xsl:when>
                    <xsl:when test="action = 20">modified</xsl:when>
                    <xsl:when test="action = 30">deleted</xsl:when>
                    <xsl:when test="action = 40">exported</xsl:when>
                    <xsl:otherwise>???</xsl:otherwise>
                </xsl:choose>
                <xsl:text> </xsl:text>
                <xsl:value-of select="table"/>
                <xsl:text> </xsl:text>
                <xsl:choose>
                    <xsl:when test="action = 40">data</xsl:when>
                    <xsl:otherwise><xsl:value-of select="row"/></xsl:otherwise>
                </xsl:choose>
            </td>
        </tr>
    </xsl:template>

    <xsl:template match="/*" priority="-1">
        <xsl:choose>
            <xsl:when test="substring (local-name(), string-length(local-name())-4)='-list'">
                <h2><xsl:value-of select="substring (local-name(), 1, string-length (local-name()) - 5)"/>s</h2>
                <xsl:call-template name="list">
                    <xsl:with-param name="id" select="'data'"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <h2><xsl:value-of select="local-name()"/></h2>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="persons">
        <xsl:call-template name="table">
            <xsl:with-param name="name" select="substring (local-name(), 1, string-length(local-name())-1)"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="item/id">
        #<xsl:value-of select="."/> - 
    </xsl:template>

    <xsl:template match="id"/>

    <xsl:template match="customer/Company">
        <i><xsl:value-of select="."/></i>
    </xsl:template>

    <xsl:template match="customer/Last-Name">
        <xsl:if test=". != ''"><xsl:value-of select="."/>,</xsl:if>
    </xsl:template>

    <xsl:template match="part/Description">
        <xsl:if test=". != ''">- <xsl:value-of select="."/></xsl:if>
    </xsl:template>


    <xsl:template name="table">
        <xsl:param name="name"/>
        <xsl:param name="nosearch"/>
        <xsl:param name="param"/>
        <xsl:param name="param2"/>
        <xsl:param name="continue">true</xsl:param>

        <xsl:variable name="extra"><xsl:if test="$param">?=<xsl:value-of select="$param"/><xsl:if test="$param2">&amp;=<xsl:value-of select="$param2"/></xsl:if></xsl:if></xsl:variable>
        <xsl:variable name="id"><xsl:value-of select="$name"/><xsl:if test="$param">_<xsl:value-of select="$param"/><xsl:if test="$param2">_<xsl:value-of select="$param2"/></xsl:if></xsl:if></xsl:variable>

        <fieldset class="data {$name}s">
            <legend>
                <h3>
                    <a href="/$/bin/data/{$name}/ls{$extra}">
                        <xsl:attribute name="onclick"><xsl:choose>
                                <xsl:when test="$nosearch"/>
                                <xsl:otherwise>get ('<xsl:value-of select="$id"/>_search').value='';</xsl:otherwise>
                        </xsl:choose> return !loadToggle (this, '<xsl:value-of select="$id"/>') </xsl:attribute>
                        <xsl:value-of select="$name"/>s
                    </a>
                </h3>
            </legend>
            <form action="/$/bin/data/{$name}/find" method="GET" autocomplete="off" onsubmit="return false">
                <xsl:if test="$param">
                    <input type="hidden" name="param" value="{$param}"/>
                    <xsl:if test="$param2">
                        <input type="hidden" name="param2" value="{$param2}"/>
                    </xsl:if>
                </xsl:if>
                <xsl:choose>
                    <xsl:when test="$nosearch"/>
                    <xsl:otherwise>
                        <input id="{$id}_search" name="query" class="search" placeholder="search" autocomplete="off" onkeyup="loadSearch (event, this, '{$id}', '/$/bin/data/{$name}/find{$extra}')"/><noscript><input type="submit" value="Find"/></noscript>
                    </xsl:otherwise>
                </xsl:choose>
            </form>
            <div id="{$id}" class="content"/>
            <a class="create" target="{$name}" href="/$/bin/data/{$name}/new{$extra}" onclick="return !loadInPopup (this)"><span class="legacy">[</span>new <xsl:value-of select="$name"/><span class="legacy">]</span></a><br class="legacy"/>
            <xsl:if test="$continue = 'true'">
                <xsl:apply-templates/>
            </xsl:if>
        </fieldset>
    </xsl:template>

    <!-- *-list -->
    <xsl:template match="*[substring (local-name(), string-length(local-name())-4)='-list']" priority="-5">
        <xsl:call-template name="list"/>
    </xsl:template>

    <xsl:template name="list">
        <xsl:param name="id"/>
        <xsl:param name="ordered"/>

        <xsl:variable name="tag">
            <xsl:choose>
                <xsl:when test="$ordered = 'true'">ol</xsl:when>
                <xsl:otherwise>ul</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:element name="{$tag}">
            <xsl:if test="$id != ''"><xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute></xsl:if>
            <xsl:variable name="name" select="substring (local-name(), 1, string-length (local-name()) - 5)"/>
            <li class="create">
                <a target="{$name}" onclick="return !loadInPopup (this)">
                    <xsl:attribute name="href">/$/bin/data/<xsl:value-of select="$name"/>/new<xsl:if test="$arg1">?=<xsl:value-of select="$arg1"/><xsl:if test="$arg2">&amp;=<xsl:value-of select="$arg2"/></xsl:if></xsl:if></xsl:attribute>
                    <span class="legacy">[</span>new <xsl:value-of select="$name"/><span class="legacy">]</span>
                </a>
            </li>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <!-- *-list/* (record) -->
    <xsl:template match="*[substring (local-name(), string-length(local-name())-4)='-list']/*" priority="-5">
        <li>
            <xsl:call-template name="record"/>
        </li>
    </xsl:template>

    <xsl:template name="record">
        <a target="{local-name()}" href="/$/bin/data/{local-name()}/view?={id}" onclick="return !loadInPopup (this)">
            <xsl:apply-templates/>
        </a>
    </xsl:template>

    <!-- *-list/*/* (column) -->
    <xsl:template match="*[substring (local-name(), string-length(local-name())-4)='-list']/*/*" priority="-1">
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

